<!--
 * @Description: 新增
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-06-10 11:11:25
 * @FilePath: /yuxiuproweb/src/views/app/image/mods/add.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    title="app详情内容维护"
    :visible.sync="shows"
    width="500px"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <!-- <el-form-item label="关联商品" prop="productId">
        <el-select
          v-model="form.productId"
          placeholder="请选择关联商品"
          style="width: 100%"
          clearable
          filterable
        >
          <el-option
            v-for="item in productList"
            :key="item.productId"
            :label="item.name"
            :value="item.productId"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="主图">
        <image-upload v-model="form.homeImage" />
      </el-form-item>
      <el-form-item label="轮播图" prop="banner">
        <image-upload v-model="form.banner" />
      </el-form-item>
      <el-form-item label="瀑布图" prop="waterfallPlot">
        <image-upload v-model="form.waterfallPlot" />
      </el-form-item>

      <!-- <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listImage, addImage, updateImage } from "@/api/app/image";
export default {
  props: {
    show: Boolean,
    type: {
      type: String,
    },
    id: {
      type: String | Number,
    },
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  mounted() {
    this.getImageData();
  },
  methods: {
    getImageData() {
      listImage({
        productId: this.id,
        type: this.type,
      }).then(({ rows }) => {
        rows?.length && ([this.form] = rows);
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.productId = this.id;
          this.form.type = this.type;
          if (this.form.productImageId != null) {
            updateImage(this.form).then((response) => {
              this.$modal.msgSuccess("保存成功");
              this.shows = false;
              this.$emit("update");
            });
          } else {
            addImage(this.form).then((response) => {
              this.$modal.msgSuccess("保存成功");
              this.shows = false;
              this.$emit("update");
            });
          }
        }
      });
    },
  },
};
</script>
